package io.hepu.robotize.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.Column;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;

/**
 * The Base entity for model
 *
 * @param <ID> primary key type
 */
public class BaseEntity<ID extends Serializable> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "ID")
    @Column(name = "id")
    protected ID id;

    @ApiModelProperty(value = "创建者")
    @Column(name = "creator")
    @JsonIgnore
    protected String creator;

    @ApiModelProperty(value = "更新者")
    @Column(name = "updater")
    @JsonIgnore
    protected String updater;

    @ApiModelProperty(value = "删除者")
    @Column(name = "deleter")
    @JsonIgnore
    protected String deleter;

    @ApiModelProperty(value = "创建日期")
    @Column(name = "create_at")
    protected Date createAt;

    @ApiModelProperty(value = "更新日期")
    @Column(name = "update_at")
    @JsonIgnore
    protected Date updateAt;

    @ApiModelProperty(value = "删除日期")
    @Column(name = "delete_at")
    @JsonIgnore
    protected Date deleteAt;

    @JsonIgnore
    private Class<?> idClass;

    public BaseEntity() {
        // Objective: get the actual type parameter
        // Get the current running object
        Class<?> clazz = this.getClass();
        // Get the parameterized type of the parent class of the current object.
        // Usually we use the type sub-interface ParameterizedType.
        Type type = clazz.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            // Get the actual type parameter
            Type[] types = parameterizedType.getActualTypeArguments();
            this.idClass = (Class<?>) types[0];
        }
    }

    public Class<?> getIdClass() {
        return idClass;
    }

    public void setIdClass(Class<?> idClass) {
        this.idClass = idClass;
    }

    public ID getId() {
        return id;
    }

    public void setId(ID id) {
        this.id = id;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator == null ? null : creator.trim();
    }

    public String getUpdater() {
        return updater;
    }

    public void setUpdater(String updater) {
        this.updater = updater == null ? null : updater.trim();
    }

    public String getDeleter() {
        return deleter;
    }

    public void setDeleter(String deleter) {
        this.deleter = deleter == null ? null : deleter.trim();
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public Date getUpdateAt() {
        return updateAt;
    }

    public void setUpdateAt(Date updateAt) {
        this.updateAt = updateAt;
    }

    public Date getDeleteAt() {
        return deleteAt;
    }

    public void setDeleteAt(Date deleteAt) {
        this.deleteAt = deleteAt;
    }
}
